﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Web.Mvc;
using Sealight.API.Framework;
using Sealight.Core.DataAdapter;

namespace Sealight.UI.WebUI.UIHelpers.Localization
{
	public abstract class LocalizedControllerBase : Controller
	{
        public abstract bool IsFrontEnd { get; set; }
		public String LanguageCode { get; private set; }

        public Guid LanguageId { get; private set; }

        public language Language { get; private set; }

        public string CurrentControllerName { get; private set; }
        public string CurrentActionName { get; private set; }
        public string CurrentId { get; set; }

		protected override void Execute(System.Web.Routing.RequestContext requestContext)
		{
            if (requestContext.RouteData.Values["languageCode"] != null)
            {
                LanguageCode = requestContext.RouteData.Values["languageCode"].ToString();
            }
            else
            {
                LanguageCode = "vi";                
            }

            if (requestContext.RouteData.Values["controller"] != null)
            {
                CurrentControllerName = requestContext.RouteData.Values["controller"].ToString();
            }

            if (requestContext.RouteData.Values["action"] != null)
            {
                CurrentActionName = requestContext.RouteData.Values["action"].ToString();
            }

            if (requestContext.RouteData.Values["id"] != null)
            {
                CurrentId = requestContext.RouteData.Values["id"].ToString();
            }

            if (LanguageCode.ToLower() == "vi")
            {
                LanguageId = GlobalConstant.LANGUAGE_CURRENT_VN;
                //ViewData["URL_LANGUAGE"] = string.Format("/en/{0}/{1}/{2}", CurrentControllerName, CurrentActionName, CurrentId);
                ViewData["URL_LANGUAGE"] = "/en/home/index";
                ViewData["menu"] = this.LoadMenuVn();
            }
            else
            {
                LanguageId = GlobalConstant.LANGUAGE_CURRENT_EN;
                //ViewData["URL_LANGUAGE"] = string.Format("/vi/{0}/{1}/{2}", CurrentControllerName, CurrentActionName, CurrentId);
                ViewData["URL_LANGUAGE"] = "/vi/home/index";
                ViewData["menu"] = this.LoadMenuEn();
            }

            Language = new language
            {
                code = LanguageCode,
                id = LanguageId
            };

			if ( !AppConfig.SupportedLanguages.Contains(LanguageCode) )
				LanguageCode = AppConfig.DefaultLanguageCode;

			System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture(LanguageCode);
			Thread.CurrentThread.CurrentCulture = culture;
			Thread.CurrentThread.CurrentUICulture = culture;

            ViewData["LanguageCode"] = LanguageCode;
            ViewData["URL_VN"] = string.Format("/vi/{0}/{1}/{2}",CurrentControllerName, CurrentActionName, CurrentId );
            ViewData["URL_EN"] = string.Format("/en/{0}/{1}/{2}", CurrentControllerName, CurrentActionName, CurrentId);

            if (requestContext.HttpContext.Request.QueryString != null && requestContext.HttpContext.Request.QueryString.Count > 0)
            {
                //ViewData["URL_LANGUAGE"] = string.Format("{0}?{1}", ViewData["URL_LANGUAGE"], requestContext.HttpContext.Request.QueryString.ToString());
                ViewData["URL_VN"] = string.Format("{0}?{1}", ViewData["URL_VN"], requestContext.HttpContext.Request.QueryString.ToString());
                ViewData["URL_EN"] = string.Format("{0}?{1}", ViewData["URL_EN"], requestContext.HttpContext.Request.QueryString.ToString());

            }

			base.Execute(requestContext);
		}

        private IList<menu> LoadMenuVn()
        {
            IList<menu> listMenu = null;

            listMenu = new List<menu>
            {
                new menu{title = "Xây dựng dân dụng & công nghiệp", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"8686a42d-bff4-4c76-870c-7f0217148ccf")},
                new menu{title = "Hệ thống điện trung thế , hạ thế, trạm biến áp", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"b526ff9d-9e59-4a18-ad2a-552efb5bba58")},
                 new menu{title = "Hệ thống điều hòa không khí, thông gió", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"bfc5c2a9-aa3a-4cee-a0fd-00a1b8949c68")},
                  new menu{title = "Hệ thống báo cháy & pccc", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"4397320f-5f1e-4f4b-ad09-ddf111a73833")},
                   new menu{title = "Hệ thống cấp thoát nước", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"af31c60d-dc79-44e2-8eed-ec51ba924671")},
                    new menu{title = "Hệ thống chống sét", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"ab914d07-c976-4b1f-9213-dbf5351cae1c")},
                     new menu{title = "Hệ thống thông tin", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"c22e87d5-2397-4b8c-a92f-31e12a044d3e")},
                      new menu{title = "Mua bán thiết bị điện", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"0f7d3a3d-bef4-4573-91be-9743ad4acb3a")}
            };

            return listMenu;
        }

        private IList<menu> LoadMenuEn()
        {
            IList<menu> listMenu = null;

            listMenu = new List<menu>
            {
                new menu{title = "Civil and industrial", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"b80992d4-79b8-4599-aee4-b8db611d0bda")},
                new menu{title = "The system is electric Medium voltage - transformers stations", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"177988b8-c653-494b-992f-70d9281036b9")},
                 new menu{title = "System air-conditioning and ventilation", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"aca88232-48f6-4774-880f-a2163355a5ee")},
                  new menu{title = "System Automatic Fire Alarm and Fire", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"de153fbd-29ba-4aef-94ee-4c90bce240b1")},
                   new menu{title = "System of water supply", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"9e565a62-b3b8-4b25-88bf-3bdf3d34ae1a")},
                    new menu{title = "The system is electric low voltage - Lightning Protection", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"92f0555b-8783-4017-ba23-98738668bc81")},
                     new menu{title = "Information systems", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"fc92db45-014a-4b9c-a500-afe7c0d60b34")},
                      new menu{title = "Buying and selling electrical equipment", navigation_url = string.Format("/{0}/project/list?categoryId={1}", this.LanguageCode,"4219f064-8610-44f6-900d-88453ad9f094")}
            };

            return listMenu;
        }
	}
}